<?php
/**
 * 消费豆及日志管理
 *
 *
 *
 * * @网店运维 (c) 2015-2018 ShopWWI Inc. (http://www.shopwwi.com)
 * @license    http://www.shopwwi.c om
 * @link       交流群号：111731672
 * @since      网店运维提供技术支持 授权请购买shopnc授权
 */
defined('ByShopWWI') or exit('Access Invalid!');

class stock_rightModel extends Model {
    
    private $stage_arr;
    public function __construct(){
        parent::__construct('stock_right_record');
        $this->stage_arr = array('lottery_draw'=>'兑换股权');
    }
   
    /**
     * 操作股权
     * @author ShopNC Develop Team
     * @param  string $stage 操作阶段 lottery_draw(兑换股权) 
     * @param  array $insertarr 该数组可能包含信息 array('br_memberid'=>'会员编号','br_membername'=>'会员名称','br_adminid'=>'管理员编号','br_adminname'=>'管理员名称','br_stock_right'=>'股权','br_desc'=>'描述');
     * @param  bool $if_repeat 是否可以重复记录的信息,true可以重复记录，false不可以重复记录，默认为true
     * @return bool
     */
    public function saveStockRightRecord($stage,$insertarr,$if_repeat = true){
        
        if (!$insertarr['br_memberid']){
            return false;
        }
        //记录原因文字
        switch ($stage){
            case 'lottery_draw':
                if (!$insertarr['br_desc']){
                    $insertarr['br_desc'] = '兑换股权';
                }
                break;
            case 'other':
                break;
        }
        $save_sign = true;
        if ($if_repeat == false){
            //检测是否有相关信息存在，如果没有，入库
            $condition['br_memberid'] = $insertarr['br_memberid'];
            $condition['br_stage'] = $stage;
            $log_array = $this->getStockRightInfo($condition);
            if (!empty($log_array)){
                $save_sign = false;
            }
        }
        if ($save_sign == false){
            return true;
        }
      
        //新增日志
        $value_array = array();
        $value_array['br_memberid'] = $insertarr['br_memberid'];
        $value_array['br_membername'] = $insertarr['br_membername'];
        if ($insertarr['br_adminid']){
            $value_array['br_adminid'] = $insertarr['br_adminid'];
        }
        if ($insertarr['br_adminname']){
            $value_array['br_adminname'] = $insertarr['br_adminname'];
        }
        $value_array['br_stock_right'] = $insertarr['br_stock_right'];
        $value_array['br_addtime'] = time();
        $value_array['br_desc'] = $insertarr['br_desc'];
        $value_array['br_stage'] = $stage;
        $result = false;
        if($value_array['br_bean'] != '0'){
            $result = $this->addStockRightRecord($value_array);
        }
       
        if ($result){
            //更新member内容
            $obj_member = Model('member');
            $upmember_array = array();
            $upmember_array['stock_right'] = array('exp','stock_right+'.$insertarr['br_stock_right']);
            $obj_member->editMember(array('member_id'=>$insertarr['br_memberid']),$upmember_array);
            return true;
        }else {
            return false;
        }
   
      
  

    }
     /**
     * 添加股权日志信息
     *
     * @param array $param 添加信息数组
     */
    public function addStockRightRecord($param) {
        if(empty($param)) {
            return false;
        }
        return $this->insert($param);
    }
    /**
     * 股权日志列表
     *
     * @param unknown $condition
     * @param string $pagesize
     * @param string $fields
     * @param string $order
     */
    public function getStockRightRecordList($condition = array(), $pagesize = '', $fields = '*', $order = '', $limit = ''){
        return $this->table('stock_right_record')->where($condition)->field($fields)->order($order)->limit($limit)->page($pagesize)->select();
    }
    
     /**
     * 取日志总数
     * @param unknown $condition
     */
    public function getStockRightRecordCount($condition = array()) {
        return $this->table('stock_right_record')->where($condition)->count();
    }
    
    
    /**
     * 股权日志详细信息
     *
     * @param array $condition 条件数组
     * @param array $field   查询字段
     */
    public function getStockRightInfo($where = array(), $field = '*', $order = '',$group = ''){
        $info = $this->table('stock_right_record')->where($where)->field($field)->order($order)->group($group)->find();
        if (!$info){
            return array();
        }
        if($info['br_stage']){
            $info['stagetext'] = $this->stage_arr[$info['br_stage']];
        }
        if ($info['br_addtime']) {
            $info['addtimetext'] = @date('Y-m-d',$info['br_addtime']);
        }
        return $info;
    }
}
